package com.bailong.nanny.controller;

import com.bailong.nanny.base.BaseServlet;
import com.bailong.nanny.entity.Nanny;
import com.bailong.nanny.entity.dto.NannyWorkDTO;
import com.bailong.nanny.service.NannyService;
import com.bailong.nanny.service.UserService;
import com.bailong.nanny.service.impl.NannyServiceImpl;
import com.bailong.nanny.service.impl.UserServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet(name = "/nanny.do",urlPatterns = "/nanny.do")
public class NannyServlet extends BaseServlet {
    private static final long serialVersionUID = 1L;


    private void selectAllNanny(HttpServletRequest req, HttpServletResponse resp)throws IOException{
        try {
            resp.setContentType("text/html;charset=UTF-8");
            List<Nanny>list = new ArrayList<Nanny>();
            NannyService nannyService = new NannyServiceImpl();
            list = nannyService.selectAllNanny();
            req.setAttribute("nanny",list);
            req.getRequestDispatcher("/nanny.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void deleteNanny(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        int id = Integer.parseInt(req.getParameter("nanny_id"));
        NannyService nannyService = new NannyServiceImpl();
        int temp = nannyService.deleteNanny(id);
        if(temp!=0){
            System.out.println("删除成功");
            req.getRequestDispatcher("/user.do?m=selectAllNanny").forward(req,resp);
        }else {
            System.out.println("删除失败");
        }

    }
    private void addUser(HttpServletRequest req, HttpServletResponse resp){

    }
    private void selectNannyWorks(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        NannyService nannyService = new NannyServiceImpl();
        List<NannyWorkDTO>list = nannyService.selectAllNannyWork();
        System.out.println(list.get(0).getNannyName());
        //把集合转换为数组
        String json = new Gson().toJson(list);
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().println(json);//通过流发送json
    }
}
